package com.etooling.framework.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author 周志荣
 * @ClassName: DateFm
 * @Description: (日期处理)
 * @date 2014年12月23日 下午3:50:06
 */
public class DateFm {
    public static Date to_date(String date_str, String fm) {
        Date date = null;
        try {
            if (date_str != null) {
                SimpleDateFormat df = new SimpleDateFormat(fm);
                date = df.parse(date_str);
            } else {
                throw new Exception("输入格式错误");
            }
        } catch (Exception e) {

        }
        return date;
    }

    public static Date rMonth(Date date_str, int m) throws Exception {
        // Timestamp transDate = new Timestamp(System.currentTimeMillis());//
        // 获取系统当前时间

        // 构建日历对象,获取上月的当日
        Calendar cal = Calendar.getInstance();
        cal.setTime(date_str);
        cal.add(Calendar.MONTH, -1);
        // 将日历对象转变为Timestamp
        Date newDate = new Date(cal.getTimeInMillis());
        return newDate;
    }

    public static String rMonth(Date date, int m, String fm) throws Exception {
        // Timestamp transDate = new Timestamp(System.currentTimeMillis());//
        // 获取系统当前时间

        // 构建日历对象,获取上月的当日
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -m);
        // 将日历对象转变为Timestamp
        Date newDate = new Date(cal.getTimeInMillis());
        return to_char(newDate, fm);
    }

    /**
     * 加上月
     *
     * @param date_str
     * @param m
     * @param fm
     * @return
     * @throws Exception
     */
    public static String addMonth(Date date, int m, String fm) throws Exception {
        // Timestamp transDate = new Timestamp(System.currentTimeMillis());//
        // 获取系统当前时间
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, m);
        // 将日历对象转变为Timestamp
        Date newDate = new Date((long) cal.getTimeInMillis());
        return to_char(newDate, fm);
    }

    public static double get_xc_hour(String start_date_str, String end_date_str) throws Exception {
        double return_day = 0.0D;
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date start_date = df.parse(start_date_str);
        Date end_date = df.parse(end_date_str);
        long beginTime = start_date.getTime();
        long endTime = end_date.getTime();
        if (endTime < beginTime) endTime += 86400000L;

        long betweenDays = (endTime - beginTime) / 60000L;

        return_day = Double.parseDouble(Long.toString(betweenDays)) / 60.0D;

        return return_day;
    }

    public static double get_xc_minute(String start_date_str, String end_date_str) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date1 = simpleDateFormat.parse(start_date_str);
        Date date2 = simpleDateFormat.parse(end_date_str);

        long d1 = df.parse(df.format(date1)).getTime();
        long d2 = df.parse(df.format(date2)).getTime();

        long diff = (d2 - d1) / 1000 / 60;

        return Double.parseDouble(Long.toString(diff));
    }

    public static double get_xc_minute(Date start_date, Date end_date) {


//        Date date1 = simpleDateFormat.parse(start_date_str);
//        Date date2 = simpleDateFormat.parse(end_date_str);

        long d1 = start_date.getTime();
        long d2 = end_date.getTime();

        long diff = (d2 - d1) / 1000 / 60;

        return Double.parseDouble(Long.toString(diff));
    }

    public static String r_day(String now_day, int add_day) throws Exception {
        String new_day = "";
        Date t_day = to_date(now_day, "yyyy-MM-dd");
        Date return_day = new Date(t_day.getTime() - (long) (add_day * 24 * 60 * 60 * 1000));
        new_day = to_char(return_day, "yyyy-MM-dd");
        return new_day;
    }

    public static String r_day(Date now_day, int add_day) throws Exception {
        String new_day = "";
        Date return_day = new Date(now_day.getTime() - (long) (add_day * 24 * 60 * 60 * 1000));
        new_day = to_char(return_day, "yyyy-MM-dd");
        return new_day;
    }

    public static String r_day(Date now_day, int add_day, String fm) throws Exception {
        String new_day = "";
        Date return_day = new Date(now_day.getTime() - (add_day * 24 * 60 * 60 * 1000));
        new_day = to_char(return_day, fm);
        return new_day;
    }

    public static Date r_Date(Date now_day, int add_day) throws Exception {

        Date return_day = new Date(now_day.getTime() - ((long) add_day * 24 * 60 * 60 * 1000));

        return return_day;
    }

    public static String add_day(String now_day, int add_day) throws Exception {
        String new_day = "";
        Date t_day = to_date(now_day, "yyyy-MM-dd");
        Date return_day = new Date(t_day.getTime() + (long) add_day * 24 * 60 * 60 * 1000);
        new_day = to_char(return_day, "yyyy-MM-dd");
        return new_day;
    }

    public static String add_day(Date now_day, int add_day) throws Exception {
        String new_day = "";
        Date return_day = new Date(now_day.getTime() + (long) add_day * 24 * 60 * 60 * 1000);
        new_day = to_char(return_day, "yyyy-MM-dd");
        return new_day;
    }

    public static Date add_Date(Date now_day, int add_day) throws Exception {

        Date return_day = new Date(now_day.getTime() + (long) add_day * 24 * 60 * 60 * 1000);

        return return_day;
    }

    public static String to_char(Date date, String fm) {
        if (date == null) {
            return "";
        }
        String returnString = "";
        SimpleDateFormat df = new SimpleDateFormat(fm);
        returnString = df.format(date);
        return returnString;
    }

    public static int get_week_day(String date) throws Exception {
        int wd = 0;
        Calendar cla = Calendar.getInstance();
        cla.setTime(to_date(date, "yyyy-MM-dd"));
        wd = cla.get(7);
        return wd;
    }

    public static String getWeekString(Date date) throws Exception {
        int wd = 0;
        Calendar cla = Calendar.getInstance();
        cla.setTime(date);
        wd = cla.get(7);
        switch (wd) {
            case 2:// 增加
                return "星期一";
            case 3:// 增加
                return "星期二";
            case 4:// 增加
                return "星期三";
            case 5:// 增加
                return "星期四";
            case 6:// 增加
                return "星期五";
            case 7:// 增加
                return "星期六";
            case 1:// 增加
                return "星期日";
            default:
                return "";
        }

    }

    // 计算增加小时
    public static Date addHour(Date now_day, int addHour) throws Exception {

        Date return_day = new Date(now_day.getTime() + addHour * 60 * 60 * 1000);

        return return_day;
    }

    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return cal.getTime();
    }

    public static Date getBeginDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return cal.getTime();
    }

    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return weekEndSta;
    }

    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return calendar.getTime();
    }

    public static Date getBeginDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth(date) - 1, 1);
        return calendar.getTime();
    }

    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return calendar.getTime();
    }

    public static Date getEndDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth(date) - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth(date) - 1, day);
        return calendar.getTime();
    }

    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    public static int getNowMonth(Date date) {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }


    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    public static void main(String[] arg) {
        DateFm df = new DateFm();

        try {
            System.out.println(DateFm.to_char(DateFm.getBeginDayOfMonth(), "yyyy-MM-dd"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}